<template>
  <div class="icons">
    <swiper :options="swiperOption">
      <!-- pages是pages函数的返回值 page是pages[0]一个
      一维八列数组 -->
      <swiper-slide v-for="(page, index) of pages" :key="index">
        <div class="icon" v-for="item of page" :key="item.id">
          <div class="img-wrapper">
            <img :src="item.imgUrl" alt="" class="img-1">
          </div>
          <p class="icon-inf">{{item.desc}}</p>
        </div>
      </swiper-slide>
    </swiper>
  </div>
</template>

<script>
// import { computed } from '@vue/runtime-core'
export default {
  name: 'HomeIcon',
  props: {
    list: Array
  },
  data () {
    return {
      swiperOption: {
        autoplay: false
      }
    }
  },
  // computed计算属性，data发生变化时执行函数
  computed: {
    pages () {
      const pages = []
      this.list.forEach((item, index) => {
        // 一页最多8个icon，多了需要分页
        const page = Math.floor(index / 8)
        // 如果该页不存在，创建该页，并把属于该页的内容压入数组
        if (!pages[page]) {
          pages[page] = []
        }
        pages[page].push(item)
      })
      return pages
    }
  }
}
</script>
<style lang="stylus" scoped>
  @import '~@/assets/varibles.styl'
  @import '~@/assets/mixins.styl'
  .icons
    overflow: hidden
    width: 100%
    height: 0
    padding-bottom:50%
    .icon
      position: relative
      float: left
      overflow: hidden
      width: 25%
      padding-bottom: 25%
      .img-wrapper
        position: absolute
        top: 0
        left: 0
        right: 0
        bottom: .4rem
        box-sizing: border-box
        padding: .15rem
        .img-1
          display: block
          height: 100%
          margin: 0 auto
      .icon-inf
        position:absolute
        left: 0
        right: 0
        bottom: 0
        height: .4rem
        line-height:.4rem
        text-align: center
        color: $darkTextColor
        ellipsis()
</style>
